<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<script type="text/javascript" charset="utf-8">
	var admin_user_datagrid;
	var admin_user_editRow = undefined;
	$(function(){
		
		
		admin_user_searchForm=$('#admin_user_searchForm').form();
		
		admin_user_datagrid=$('#admin_user_datagrid').datagrid({
			url:'${pageContext.request.contextPath}/tuserAction!DataGrid.action',
			fit:false,
			fitColumns:true,
			pagination:true,
			pagePosition:'bottom',
			pageSize:5,
			pageList:[5,10,15,20,25],
			sortName:'userName',
			sortOrder:'desc	',
			idField:'userId',
			frozenColumns:[[
				{
					title:'编号',
					field:'userId',
					width:100,
					checkbox : true,
					sortable : true
				},
				{
					title:'用户姓名',
					field:'userName',
					width:100,
					sortable:'true',
					editor:{
						type:'validatebox',
						options:{
							required:true
						}
					}
				}
			]],
			columns:[[
				{
					title:'密码',
					field:'password',
					width:100,
					editor:{
						type:'validatebox',
						options:{
							required:true
						}
					}
				},
				{
					title:'所属组',
					field:'userGroupName',
					width:100,
					editor:{
						type:'combobox',
						options:{
							url : 'tuserGroupAction!checkAllGroup.action',
							valueField : 'userGroupName',
							textField : 'userGroupName'
						}
					}
				},
				{
					title:'部门编号',
					field:'deptid',
					width:100,
					editor:{
						type:'combobox',
						options:{
							url:'',
							valueField:'',
							textField:''
						}
					}
				},
				{
					title:'入职时间',
					field:'createDateTime',
					width:100,
					editor:{
						type:'datetimebox',
						options:{
							required:true,
							editable:false  
						}
					}
				}
			]],
			toolbar:[
				{
					text:'增加',
					iconCls:'icon-add',
					handler:function(){
						changeEditorAddRow();
						 if(admin_user_editRow!=undefined){
							admin_user_datagrid.datagrid('endEdit',admin_user_editRow);
						}
						if(admin_user_editRow==undefined){
							admin_user_datagrid.datagrid('insertRow',{
								index: 0,	// 索引从0开始
								row: {
									userId:UUID(),
									userName:'请输入名称',
									password:'请输入密码',
								}
								
							});
							admin_user_datagrid.datagrid('beginEdit',0);
							admin_user_editRow=0;
							//var rows=admin_user_datagrid.datagrid('getRows');
						}
					}
				},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){
						var rows=admin_user_datagrid.datagrid('getSelections');
						console.info(rows.length);
						if(rows.length>0){
							$.messager.confirm('请确认','确定要删除已选择的所有行吗？',function(b){
								if(b){
									var ids=[];
									/* var index=[]; */
									for ( var i = 0; i < rows.length; i++) {
										ids.push(rows[i].userId);
										/* index.push(admin_user_datagrid.datagrid('getRowIndex',rows[i]));
										console.info(index[i]); */
									}
									$.ajax({
										url:'${pageContext.request.contextPath}/tuserAction!delete.action',
										data:{
											ids:ids.join(','),
										},
										dataType:'json',
										success:function(j){
											if(j&&j.success){
												/* for(var i =0;i<index.length;i++){
													admin_user_datagrid.datagrid('deleteRow',0);
												} */
												
												$.messager.show({
													title:'提示',
													msg:j.msg,
												});
												admin_user_datagrid.datagrid('load');
												admin_user_datagrid.datagrid('unselectAll');
											}else{
												$.messager.alert('提示',j.msg,'warning');
											}
										}
									});
								}
							});
						
						}else{
							$.messager.alert('提示','请选择要删除的记录','warning');
						}
					}
				},'-',{
					text:'修改',
					iconCls:'icon-edit',
					handler:function(){
						changeEditoradmin_user_editRow();
						var rows=admin_user_datagrid.datagrid('getSelections');
						if(rows.length==1){
							if(admin_user_editRow!=undefined){
								admin_user_datagrid.datagrid('endEdit',admin_user_editRow);
							}
							if(admin_user_editRow==undefined){
								var index=admin_user_datagrid.datagrid('getRowIndex',rows[0]);
								admin_user_datagrid.datagrid('beginEdit',index);
								admin_user_datagrid.datagrid('unselectAll');
								admin_user_editRow=index;
							}
						}
					}
				},'-',{
					text:'保存',
					iconCls:'icon-save',
					handler:function(){
						admin_user_datagrid.datagrid('endEdit',admin_user_editRow);
					}
				},'-',{
					text:'取消编辑',
					iconCls:'icon-undo',
					handler:function(){
						admin_user_editRow=undefined;
						admin_user_datagrid.datagrid('rejectChanges');
						admin_user_datagrid.datagrid('unselectAll');
					}
				},'-',{
					text:'全选',
					iconCls:'icon-redo',
					handler:function(){
						admin_user_editRow=undefined;
						admin_user_datagrid.datagrid('selectAll');
					}
				},],
				onAfterEdit:function(rowIndex,rowData,changes){
					console.info(rowData);
					admin_user_editRow=undefined;
					var insert=admin_user_datagrid.datagrid('getChanges','inserted');
					console.info(insert);
					var update=admin_user_datagrid.datagrid('getChanges','updated');
					console.info(update);
					var url='';
					if(insert.length>0){
						url='${pageContext.request.contextPath}/tuserAction!addUser.action';
					}
					if(update.length>0){
						url='${pageContext.request.contextPath}/tuserAction!update.action';
					}
					$.ajax({
							url:url,
							type:'POST',
							data:rowData,
							dataType:'json',
							success:function(j){
								if(j&&j.success){
									admin_user_datagrid.datagrid('acceptChanges');
									admin_user_datagrid.datagrid('load');
									$.messager.show({
										msg:j.msg,
										title:'提示'
									});
								}else{
									admin_user_datagrid.datagrid('rejectChanges');
									$.messager.show({
										msg:j.msg
									});
								}
							}
						});
				},
				onDblClickRow:function(rowIndex,rowData){
					changeEditoradmin_user_editRow();
					if(admin_user_editRow!=undefined){
						admin_user_datagrid.datagrid('endEdit',admin_user_editRow);
					}
					if(admin_user_editRow==undefined){
						admin_user_datagrid.datagrid('beginEdit',rowIndex);
						admin_user_editRow=rowIndex;
					}
				}
		});
		
		checkUser=function(){
			admin_user_datagrid.datagrid('load',serializeObject(admin_user_searchForm));
		};
		clean=function(){
			admin_user_datagrid.datagrid('load',{});
			
			//$('input not(name=hidden)').val('');
			admin_user_searchForm.find('input').val('');
			admin_user_searchForm.find('[name=hidden]').val('1');
		};
		changeEditorAddRow=function(){/*添加行时改变Editor*/
			admin_user_datagrid.datagrid('addEditor',{
							field:'password',
							editor:{
								type:'validatebox',
								options:{
									required:true
								}
							}
						});
			//admin_user_datagrid.datagrid('removeEditor','createDateTime');
		};
		changeEditoradmin_user_editRow=function(){/*修改行时改变Editor*/
			admin_user_datagrid.datagrid('removeEditor',['password','createDateTime']);
			
		};
	});
</script>
<div class="easyui-layout" fit="true" border="false">
	<div region="north" border="false" title="条件查询" style="height: 110px;overflow: hidden;">
		<form id="admin_user_searchForm">
			<table style="width: 100%;height: 100%;"  class="datagrid-toolbar">
				<tr>
					<th align="right">用户名</th>
					<td align="left"><input name="userName"/></td>
				</tr>
				<tr>
					<td align="left"><input name="hidden" type="hidden" value="1"/></td>
				</tr>
				<tr>
					<th align="right">部门编号</th>
					<td align="left">
						<input name="deptid"/>
					</td>
				</tr>
				<tr>
					<th align="right">入职时间</th>
					<td align="left">
						<input name="createDateTimeStrat" editable="fasle" class="easyui-datetimebox"/>至<input name="createDateTimeEnd" editable="fasle" class="easyui-datetimebox"/>
						<a href="javascript:void(0);" class="easyui-linkbutton" onclick="checkUser()">查询</a>
						<a href="javascript:void(0);" class="easyui-linkbutton" onclick="clean()">清空</a>
					</td>
				</tr>
			</table>
		</form>
	</div>
	<div region="center" border="fasle">
		<table id="admin_user_datagrid">
		</table>
	</div>
</div>
